<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2010 - 2011         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Product extends Controller
{
    function __construct()
    {
        parent::Controller();
        $this->lang->load('home/global');
    }
    
    function index()
    {
        #BEGIN: Action
        #END Action
        #BEGIN: Bar
        #END Bar
        #BEGIN: getVar
        if(trim(strtoupper($this->uri->segment(2))) == 'SEARCH')
        {
            $segment = 3;
        }
        else
        {
            $segment = 2;
        }
        $segments = array('section', 'category', 'name', 'title', 'hot', 'bestShop', 'vip', 'reliable', 'saleOff', 'fromPrice', 'toPrice', 'fromSalary', 'toSalary', 'currency', 'unitSalary', 'fromView', 'toView', 'address', 'province', 'fromDate', 'toDate', 'search', 'keyword', 'sort', 'by', 'page');
        $getVar = $this->uri->uri_to_assoc($segment, $segments);
        #END getVar
        $sql = "FROM {PRE}product ";
        $sql .= "INNER JOIN {PRE}user ON {PRE}product.proUser = {PRE}user.useID ";
        $sql .= "INNER JOIN {PRE}shop ON {PRE}user.useID = {PRE}shop.shoUser ";
        $sql .= "INNER JOIN {PRE}section ON {PRE}product.proSection = {PRE}section.secID ";
        $sql .= "INNER JOIN {PRE}province ON {PRE}product.proProvince = {PRE}province.preID ";
        $sql .= "WHERE proStatus = 1 AND proExpireDate >= ".time()." ";
        #BEGIN: Section & Category & Search
        if(trim(strtoupper($this->uri->segment(2))) == 'SECTION')
        {
            $sql .= "AND proSection = ".(int)$getVar['section']." ";
            if(isset($getVar['category']) && (int)$getVar['category'] > 0)
            {
                $sql .= "AND proCategory = ".(int)$getVar['category']." ";
            }
        }
        else
        {
            if(isset($getVar['section']) && $getVar['section'] !== FALSE && (int)$getVar['section'] > 0)
            {
            	$sql .= "AND proSection = ".(int)$getVar['section']." ";
            }
            if(isset($getVar['category']) && $getVar['category'] !== FALSE && (int)$getVar['category'] > 0)
            {
            	$sql .= "AND proCategory = ".(int)$getVar['category']." ";
            }
            if(isset($getVar['name']) && $getVar['name'] !== FALSE && trim($getVar['name']) != '')
            {
            	$sql .= "AND proName LIKE '%".$this->db->escape_like_str($getVar['name'])."%' ";
            }
            if(isset($getVar['hot']) && $getVar['hot'] !== FALSE && (int)$getVar['hot'] == 1)
            {
            	$sql .= "AND proHot > ".time()." ";
            }
            if(isset($getVar['bestShop']) && $getVar['bestShop'] !== FALSE && (int)$getVar['bestShop'] == 1)
            {
            	$sql .= "AND proBestShop > ".time()." ";
            }
            if(isset($getVar['saleOff']) && $getVar['saleOff'] !== FALSE && (int)$getVar['saleOff'] == 1)
            {
            	$sql .= "AND proSaleOff = 1 ";
            }
            if(isset($getVar['fromPrice']) && $getVar['fromPrice'] !== FALSE && trim($getVar['fromPrice']) != '')
            {
            	$sql .= "AND proPrice >= ".(int)$getVar['fromPrice']." ";
            }
            if(isset($getVar['toPrice']) && $getVar['toPrice'] !== FALSE && trim($getVar['toPrice']) != '')
            {
            	$sql .= "AND proPrice <= ".(int)$getVar['toPrice']." ";
            }
            if(isset($getVar['currency']) && $getVar['currency'] !== FALSE && (trim(strtoupper($getVar['currency'])) == 'VND' || trim(strtoupper($getVar['currency'])) == 'USD'))
            {
            	$sql .= "AND proCurrency = '".trim(strtoupper($getVar['currency']))."' ";
            }
            if(isset($getVar['province']) && $getVar['province'] !== FALSE && (int)$getVar['province'] >= 0)
            {
            	$sql .= "AND proProvince = ".(int)$getVar['province']." ";
            }
            if(isset($getVar['fromDate']) && $getVar['fromDate'] !== FALSE && trim($getVar['fromDate']) != '')
            {
            	$sql .= "AND proPostDate >= ".date_convert($getVar['fromDate'], false, false, 1)." ";
            }
            if(isset($getVar['toDate']) && $getVar['toDate'] !== FALSE && trim($getVar['toDate']) != '')
            {
            	$sql .= "AND proPostDate <= ".date_convert($getVar['toDate'], false, false, 2)." ";
            }
        }
        #END Section & Category & Search
        #BEGIN: Sort
        if(isset($getVar['sort']) && $getVar['sort'] !== FALSE && trim($getVar['sort']) != '' && isset($getVar['by']) && $getVar['by'] !== FALSE && (trim(strtoupper($getVar['by'])) == 'ASC' || trim(strtoupper($getVar['by'])) == 'DESC'))
        {
            switch(trim(strtoupper($getVar['sort'])))
            {
                case 'NAME':
                default:
                    $sql .= "ORDER BY proID ";
            }
            $sql .= "'".trim(strtoupper($getVar['by']))."' ";
        }
        #END Sort
        #BEGIN: Page
        if(isset($getVar['page']) && $getVar['page'] !== FALSE && (int)$getVar['page'] > 0)
        {
            $start = (int)$getVar['page'];
        }
        else
        {
            $start = 0;
        }
        #END Page
        #BEGIN: Query
        $data['total'] = count($this->global_model->fetch("SELECT proID ".trim($sql)));
        $sql = "SELECT * ".trim($sql)." LIMIT $start,".PRODUCT_ROWS;
        $data['rows'] = $this->global_model->fetch($sql);
        #END Query
        #Load View
        $this->load->view('home/defaults', $data);
    }
}